package model.hospital;

import net.passClass.PassData;

import java.util.ArrayList;
import java.util.Date;

public class Equipment {
    private final ArrayList<EquipmentRequest> requests = new ArrayList<>();
    private int capacity;

    public Equipment(int capacity) {
        this.capacity = capacity;
    }

    public Equipment() {
    }

    public PassData request(EquipmentRequest request){
        if(checkNumber()>=capacity){
            Date earliest = checkEarlist();
            String str = "房间已满，请求失败。最近空闲时间：" + earliest;
            return new PassData(false,null,str);
        }
        requests.add(request);
        return new PassData(true,null,"请求成功");
    }

    private Date checkEarlist(){
        Date ans = requests.get(0).getCheck_out();
        for(EquipmentRequest request : requests){
            if(request.getCheck_out().before(ans)){
                ans = request.getCheck_out();
            }
        }
        return ans;
    }

    private int checkNumber(){
        int ans = 0;
        for(EquipmentRequest request : requests){
            if(request.getCheck_out().after(new Date())){
                ++ans;
            }
        }
        return ans;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(Integer capacity) {
        this.capacity = capacity;
    }
}
